/**
 * 
 */
package com.runningfox.Process;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.runningfox.humor.AbstractProcess;
import com.runningfox.humor.Action;
import com.runningfox.humor.ProcessCallback;
import com.runningfox.logic.Logic;
import com.runningfox.model.ResultSet;

/**
 * @author luhaiyang
 * request the detail page of joke with the joke id
 */
public class MoreJokesProcess extends AbstractProcess implements ProcessCallback{

//	String page;
	String hid;
	boolean isLocal=true;
	public MoreJokesProcess(Handler handler,String hid) {
		super(handler);
		// TODO Auto-generated constructor stub
//		this.page =page;
		this.hid=hid;
	}

	/* (non-Javadoc)
	 * @see com.runningfox.Humor.AbstractProcess#run()
	 */
	@Override
	public void runImpl() {
		// TODO Auto-generated method stub
//		Logic.requestJokesByPage(this, page);
		//先从本地获取更多笑话内容，如果本地没有，尝试从网络分页接口获取，并更新本地数据库。
		if(!Logic.requestMoreJokesFromDataBase(hid, this)){
			this.isLocal=false;
			Log.v("debug", "more jokes from network");
			Logic.requestMoreJokesById(this, hid);
		}else{
			Log.v("debug", "more jokes from local");
		}
	}

	@Override
	public void onFailed(ResultSet res) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.NOTLOADEDHOMEPAGE);
	}

	@Override
	public void onSucess(ResultSet res) {
		// TODO Auto-generated method stub
//		String html = res.viewHtml;
//		Message msg = new Message();
//		Bundle data = new Bundle();
//		msg.what = Action.VIEW;
//		data.putString("res", html);
//		msg.setData(data);
//		handler.sendMessage(msg);
		
		Message msg = new Message();
		Bundle data = new Bundle();
		msg.what = Action.MORENET;
		data.putParcelableArrayList("list", res.jokeList);
		data.putInt("size", res.size);
//		data.putString("page", res.page);
		msg.setData(data);
		handler.sendMessage(msg);
//		this.actionUpateCache(list);
		if(!this.isLocal){
			Logic.updateAllJokes(res.jokeList);
		}
	}



	
	@Override
	public void onHttp200() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTP200);		
	}

	@Override
	public void onHttp404() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTP404);
	}

	@Override
	public void onHttp500() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTP500);
	}

	@Override
	public void onHttpFaild() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.HTTPFAIL);
	}

	@Override
	public void onParsing() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.PARSING);
	}

	@Override
	public void onParseFail() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(Action.PARSEFAILED);
	}

}
